package com.ruoyi.web.controller.module.market;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.market.domain.Banner;
import com.ruoyi.market.service.IBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 轮播信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/market/banner")
public class BannerController extends BaseController
{
    @Autowired
    private IBannerService bannerService;

    /**
     * 获取轮播列表
     */
    @PostMapping("/bannerList")
    public TableDataInfo list(@RequestBody Banner banner, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            banner.setShopId(shopId);
        }
        startPageByPost();
        List<Banner> list = bannerService.selectBannerList(banner);
        return getDataTable(list);
    }

    @Log(title = "轮播管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('market:banner:export')")
    @PostMapping("/bannerExport")
    public void export(HttpServletResponse response, Banner banner)
    {
        List<Banner> list = bannerService.selectBannerList(banner);
        ExcelUtil<Banner> util = new ExcelUtil<Banner>(Banner.class);
        util.exportExcel(response, list, "轮播数据");
    }

    @Log(title = "轮播管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('market:banner:import')")
    @PostMapping("/bannerImportData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Banner> util = new ExcelUtil<Banner>(Banner.class);
        List<Banner> bannerList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = bannerService.importBanner(bannerList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/bannerImportTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Banner> util = new ExcelUtil<Banner>(Banner.class);
        util.importTemplateExcel(response, "轮播数据");
    }

    /**
     * 根据轮播编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('market:banner:query')")
    @GetMapping(value = { "/", "/{bannerId}" })
    public AjaxResult getInfo(@PathVariable(value = "bannerId", required = false) Long bannerId)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(bannerId))
        {
            Banner sysBanner = bannerService.selectBannerById(bannerId);
            ajax.put(AjaxResult.DATA_TAG, sysBanner);
        }
        return ajax;
    }

    /**
     * 新增轮播
     */
    @PreAuthorize("@ss.hasPermi('market:banner:add')")
    @Log(title = "轮播管理 - 新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Banner banner, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            banner.setShopId(shopId);
        }
        banner.setCreateBy(getUsername());
        return toAjax(bannerService.insertBanner(banner));
    }

    /**
     * 修改轮播
     */
    @PreAuthorize("@ss.hasPermi('market:banner:edit')")
    @Log(title = "轮播管理 - 修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Banner banner)
    {
        banner.setUpdateBy(getUsername());
        return toAjax(bannerService.updateBanner(banner));
    }

    /**
     * 删除轮播
     */
    @PreAuthorize("@ss.hasPermi('market:banner:remove')")
    @Log(title = "轮播管理 - 删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bannerIds}")
    public AjaxResult remove(@PathVariable Long[] bannerIds)
    {
        return toAjax(bannerService.deleteBannerByIds(bannerIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('market:banner:edit')")
    @Log(title = "轮播管理", businessType = BusinessType.UPDATE)
    @PutMapping("/bannerChangeStatus")
    public AjaxResult changeStatus(@RequestBody Banner banner)
    {
        banner.setUpdateBy(getUsername());
        return toAjax(bannerService.updateBannerStatus(banner));
    }
}
